//
//  UIBarButtonItem+Extension.swift
//  WeiBoApp
//
//  Created by James on 2017/6/12.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    //抽取便利构造函数
    
    /// 快速构造导航按钮
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - normalColor: 正常颜色
    ///   - highlightColor: 高亮颜色
    ///   - target: 目标对象
    ///   - action: 执行的方法
    ///   - isBack: 是否是返回按钮
    convenience init(title:String,normalColor:UIColor,highlightColor:UIColor,target:AnyObject?,action:Selector,isBack:Bool = false) {
        
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 70, height: 35))
        btn .setTitle(title, for: .normal)
        btn.setTitleColor(normalColor, for: .normal)
        btn.setTitleColor(highlightColor, for: .highlighted)
        
        btn.addTarget(target, action: action, for: .touchUpInside)
        
        if isBack {
            let imageName = "navigationbar_back_withtext"
            
            btn.setImage(UIImage(named:imageName) , for: .normal)
            btn.setImage(UIImage(named:"navigationbar_back_withtext_highlighted") , for: .highlighted)
        }
        btn.sizeToFit() 
        self.init(customView: btn)// 遍历构造函数 需要调用self.init
    }
}
